import Request from '@/util/pocky-request/index'

const upload = {}

upload.submit = function _submit(res, essayType, mainId){
	let result = {
		mainId: mainId,
		essayType: essayType,
		isSingleAudio: res.isCheckedWork,
		title: res.title.val,
		items: []
	}
	res.dtl.map((item, index)=>{
		result.items = [...result.items, ...getItmes(item)]
	})
	return Request().request({
		url: 'api/Essay/CreateEssay',
		method:"PUT",
		data: result
	})	
}

upload.update = function (res, essayType){
	let result = {
		title: res.title.val,
		id: res.id,
		items: []
	}
	res.dtl.map((item, index)=>{
		result.items = [...result.items, ...getItmes(item)]
	})
	return Request().request({
		url: 'api/Essay/UpdateEssay',
		method:"POST",
		data: result
	})
}

function getItmes(item){
	let res = []
	switch(item.type){
		case 'text':
			res.push({ essayItemType: 1, text: item.val })
			break
		case 'image':
			res.push({ essayItemType: 2, imageSrc: item.resSrc[0], text: item.val })
			break
		case 'record':
			res.push({ essayItemType: 3, audioSrc: item.resSrc[0], text: item.val, duration: Math.ceil(item.duration) })
			break
		case 'video':
			res.push({ essayItemType: 4, videoSrc: item.resSrc[0], text: item.val })
			break
	}
	return res
}

upload.upload = async function _upload(type, filePaths,dir) {
	//视频上传新增加载动画
	if(type == 2){
		uni.showLoading({
			mask: true,
			title: '上传中'
		})
	}
	if (!filePaths || filePaths.length == 0) {
		uni.showToast({
			title: '请选择文件',
			mask: true
		});
		return false
	}
	var result = await Request().request({
		url: 'api/CommonTools/CreateUploadParm',
		data: {
			StoreContent: type || 1
		}
	});
	var imagesPath = new Array();
	for (let item of filePaths) {
		var fileName = getName(32) + item.substr(item.lastIndexOf('.'));
		if(dir) fileName=dir+'/'+fileName;
		var res = await uni.uploadFile({
			url: result.url,
			name: 'file',
			filePath: item,
			formData: {
				key: fileName,
				success_action_status: 200,
				policy: result.policy,
				OSSAccessKeyId: result.accessKeyId,
				signature: result.sign
			}
		})
		imagesPath.push(fileName);
	}
	uni.hideLoading()
	return imagesPath;
}
upload.uploadByBase64 = async function _uploadByBase64(type,filePaths){
	if (!filePaths || filePaths.length == 0) {
		uni.showToast({
			title: '请选择文件',
			mask: true
		});
		return false
	}
	var imagesPath = new Array();
	for(let item of filePaths)
	{
		
		var fileName=await Request().request({
			url:'api/CommonTools/UploadFileByBase64',
			data:{Base64:item},
			method:"PUT"
		})
		imagesPath.push(fileName);
	}
	return imagesPath;
}


function getName(len, radix) {
	var chars = '0123456789abcdefghijklmnopqrstuvwxyz'.split('');
	var uuid = [],
		i;
	radix = radix || chars.length;
	if (len) {
		for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}
	return uuid.join('');
}

export default upload
